from globals import *
from racer import racer

class world(DirectObject):#subclassing is necessary to accept events
    def __init__(self):
        #Declare some fancy local variables here.
        self.isMoving = False
        self.prevTime = 0
        self.win = False
        self.lost = False
        
        self.setAccept()
        
        #1  2
        #
        #1  2
        # 3
        #
        #1  2
        #3  4
        #
        #create the racers
         # car is a string
        if(numRacers == 2):
            self.racerA = racer(carA, (0,.5,0,1), False)
            self.racerB = racer(carB, (.5,1,0,1), False)
        if(numRacers == 3):
            self.racerA = racer(carA, (0,.5,.5,1), False)
            self.racerB = racer(carB, (.5,1,.5,1), False)
            self.racerC = racer(carC, (0,.5,0,.5), False)
        if(numRacers == 4):
            self.racerA = racer(carA, (0,.5,.5,1), False)
            self.racerB = racer(carB, (.5,1,.5,1), False)
            self.racerC = racer(carC, (0,.5,0,.5), False)
            self.racerD = racer(carD, (.5,1,0,.5), False)
        
        #Call load models to load all players/environment
        self.loadModels()
        
        #base.disableMouse() #turn off mouse control
        #base.camNode.setActive(False) #disable default cam
        
        render.setShaderAuto()
        #camera.setPosHpr(0,-15,7,0,-15,0)
        
        #create some awesome lighting
        self.setupLights()
        
        #set up collisions
        #self.setupCollisions()
        
        #set up music
        #self.music = loader.loadMusic("sounds/rocky.mp3")
        #SoundInterval(self.music).loop()
        
        
        
        #camera.LookAt(self.racer)
        taskMgr.add(self.move, "moveTask")

    def fire_long(self, cEntry):
        self.racer.play("eat")
        self.eatSound.play()
        self.targets.remove(cEntry.getIntoNodePath().getParent())
        cEntry.getIntoNodePath().getParent().remove()
        
    def has_won(self):
        self.win = True
        return self.win
        
    def has_lost(self):
        self.lost = True
        return self.lost
        
    def fire_short(self, cEntry):
        self.racer.play("eat")
        self.eatSound.play()
    
    def hitTrack(self,cEntry):
        pass
        self.play("")
    def hitArena(self,cEntry):
        pass
        self.play("")
    def hitBullet(self,cEntry):
        pass
        self.play("")
    def hitMelee(self,cEntry):
        pass
        self.play("")
    def hitRacer(self,cEntry):
        pass
        self.play("")
    def loadModels(self):
        """loads models, duh"""
        #first set up the environment
        self.env = loader.loadModel("models/track")
        self.env.reparentTo(render)
        self.env.setScale(1)
        self.env.setPos(0,0,0)
        
        #self.racer.setControlEffect("accelerate",1)
        #self.racer.setControlEffect("turn_left",1)
        #self.racer.setControlEffect("turn_right",1)
        #self.racer.setControlEffect("brake",1)
        #self.racer.setControlEffect("fire_short",1)
        #self.racer.setControlEffect("fire_long",1)
        #self.racer.setScale(.005)
        #self.racer.setH(180)
        #self.racerAccelerate = self.racer.posInterval(1,(0,-5,0))

    def setupLights(self):
        """Sets intitial lighting"""
        self.dirLight = DirectionalLight("dirLight")
        self.dirLight.setColor((.6,.6,.6,1))#Sets color of the directional light, alpha means nothing
        #create node path and connect directly to the scene
        self.dirLightNP = render.attachNewNode(self.dirLight) #paths to node
        self.dirLightNP.setHpr(0,-26,0) #sets light rotation
        #the nodepath that calls setLight is what gets illuminated by the light, will call on render
        render.setLight(self.dirLightNP)
        #use clearLight() to turn it off
        
        self.ambientLight = AmbientLight("ambientLight")
        self.ambientLight.setColor((.25,.25,.25,1))
        self.ambientLightNP = render.attachNewNode(self.ambientLight)
        render.setLight(self.ambientLightNP)
        
    def setAccept(self):
        # Control inputs
        self.accept("enter", self.setKey, ["enter", 1])
        
        self.accept("arrow_up", self.setKey, ["forwardA", 1])
        self.accept("arrow_left", self.setKey, ["leftA", 1])
        self.accept("arrow_right", self.setKey, ["rightA", 1])
        self.accept("arrow_down", self.setKey, ["brakeA", 1])
        self.accept("mouse1", self.setKey, ["fireA", 1])
        
        self.accept("w", self.setKey, ["forwardB", 1])
        self.accept("a", self.setKey, ["leftB", 1])
        self.accept("d", self.setKey, ["rightB", 1])
        self.accept("s", self.setKey, ["brakeB", 1])
        self.accept("space", self.setKey, ["fireB", 1])
        
        self.accept("t", self.setKey, ["forwardC", 1])
        self.accept("f", self.setKey, ["leftC", 1])
        self.accept("h", self.setKey, ["rightC", 1])
        self.accept("g", self.setKey, ["brakeC", 1])
        self.accept("b", self.setKey, ["fireC", 1])
        
        self.accept("i", self.setKey, ["forwardD", 1])
        self.accept("j", self.setKey, ["leftD", 1])
        self.accept("l", self.setKey, ["rightD", 1])
        self.accept("k", self.setKey, ["brakeD", 1])
        self.accept("m", self.setKey, ["fireD", 1])
        
        self.accept("hit-track",self.hitTrack)
        self.accept("hit-arena",self.hitArena)
        self.accept("hit-racer",self.hitRacer)
        self.accept("hit-bullet",self.hitBullet)
        self.accept("hit-melee",self.hitMelee)
        
        #stop moving events
        self.accept("enter-up", self.setKey, ["enter", 1])
        
        self.accept("arrow_up-up", self.setKey, ["forwardA", 0])
        self.accept("arrow_left-up", self.setKey, ["leftA", 0])
        self.accept("arrow_right-up", self.setKey, ["rightA", 0])
        self.accept("arrow_down-up", self.setKey, ["brakeA", 0])
        self.accept("mouse1-up", self.setKey, ["fireA", 0])
        
        self.accept("w-up", self.setKey, ["forwardB", 0])
        self.accept("a-up", self.setKey, ["leftB", 0])
        self.accept("d-up", self.setKey, ["rightB", 0])
        self.accept("s-up", self.setKey, ["brakeB", 0])
        self.accept("space-up", self.setKey, ["fireB", 0])
        
        self.accept("t-up", self.setKey, ["forwardC", 0])
        self.accept("f-up", self.setKey, ["leftC", 0])
        self.accept("h-up", self.setKey, ["rightC", 0])
        self.accept("g-up", self.setKey, ["brakeC", 0])
        self.accept("b-up", self.setKey, ["fireC", 0])
        
        self.accept("i-up", self.setKey, ["forwardD", 0])
        self.accept("j-up", self.setKey, ["leftD", 0])
        self.accept("l-up", self.setKey, ["rightD", 0])
        self.accept("k-up", self.setKey, ["brakeD", 0])
        self.accept("m-up", self.setKey, ["fireD", 0])
    
    def move(self,task):#null methods added to the task manager get passed task info
        """move racer A through D depending on the keymap"""
        elapsed = task.time - self.prevTime
        if (keyMap["leftA"] != 0):
            self.racerA.racer.setH(self.racerA.racer.getH() + elapsed * 100)
        if (keyMap["rightA"] != 0):
            self.racerA.racer.setH(self.racerA.racer.getH() - elapsed * 100)
        if (keyMap["forwardA"] != 0):
            self.racerA.racer.setY(self.racerA.racer, -25)

        #if keyMap["leftA"] or keyMap["rightA"] or keyMap["forwardA"]:
        #    if self.isMoving == False:
        #        self.isMoving = True
        #        self.racerA.racer.play("turnLeft")
        #else:
        #    if self.isMoving:
        #        self.isMoving = False
        #        self.racerA.racer.stop()
        #        self.racerA.racer.pose("walk",4)
        self.prevTime = task.time
        
        return Task.cont
        
    # def setupCollisions(self):
    #        #Makes a collision traverser and sets it the default
    #        base.cTrav = CollisionTraverser()
    #        #sets the collision handler to one that generates collision events
    #        self.cHandler = CollisionHandlerEvent()
    #        #set the pattern for the event sent on collision
    #        #"%in" is substituted with the name of the "into" object
    #        self.cHandler.setInPattern("hit-%in")
    #        
    #        #Remember; Panda is scaled WAY down, so diameter must be too
    #        cSphere_A = CollisionSphere((0,0,0), 500)
    #        cSphere_B = CollisionSphere((0,0,0), 500)
    #        if(numRacers == 3 or numRacers == 4):
    #            cSphere_C = CollisionSphere((0,0,0), 500)
    #        if(numRacers == 4):
    #            cSphere_D = CollisionSphere((0,0,0), 500)
    #        
    #        cNode_A = CollisionNode("racerA")
    #        cNode_B = CollisionNode("racerB")
    #        if(numRacers == 3 or numRacers == 4):
    #            cNode_C = CollisionNode("racerC")
    #        if(numRacers == 4):
    #            cNode_D = CollisionNode("racerD")
    #        
    #        cNode_A.addSolid(cSphere_A)
    #        cNode_B.addSolid(cSphere_B)
    #        if(numRacers == 3 or numRacers == 4):
    #            cNode_C.addSolid(cSphere_C)
    #        if(numRacers == 4):
    #            cNode_D.addSolid(cSphere_D)
    #        
    #        cNode_A.setIntoCollideMask(BitMask32.allOff()) #Because the Panda is ONLY a "from" object
    #        cNode_B.setIntoCollideMask(BitMask32.allOff())
    #        if(numRacers == 3 or numRacers == 4):
    #            cNode_C.setIntoCollideMask(BitMask32.allOff())
    #        if(numRacers == 4):
    #            cNode_D.setIntoCollideMask(BitMask32.allOff())
    #        
    #        cNodePath_A = self.racerA.racer.attachNewNode(cNode_A)
    #        cNodePath_B = self.racerB.racer.attachNewNode(cNode_B)
    #        if(numRacers == 3 or numRacers == 4):
    #            cNodePath_C = self.racerC.racer.attachNewNode(cNode_C)
    #        if(numRacers == 4):
    #            cNodePath_D = self.racerD.racer.attachNewNode(cNode_D)
    #        
    #        #cNode.setIntoCollideMask(BitMask32.allOff()) #Because the Panda is ONLY a "from" object
    #        cNodePathA = self.racerA.racer.attachNewNode(cNode_A)
    #        cNodePathB = self.racerB.racer.attachNewNode(cNode_B)
    #        if(numRacers == 3 or numRacers == 4):
    #            cNodePathC = self.racerC.racer.attachNewNode(cNode_C)
    #        if(numRacers == 4):
    #            cNodePathD = self.racerD.racer.attachNewNode(cNode_D)
    #        #cNodePath.show()
    #        base.cTrav.addCollider(cNodePathA, self.cHandler)
    #        base.cTrav.addCollider(cNodePathB, self.cHandler)
    #        if(numRacers == 3 or numRacers == 4):
    #            base.cTrav.addCollider(cNodePathC, self.cHandler)
    #        if(numRacers == 4):
    #            base.cTrav.addCollider(cNodePathD, self.cHandler)
    #        
            
            